package com.wyj.databinding

import android.os.Looper
import androidx.databinding.ViewDataBinding

interface ILoadingDialogEvent {
    val loadingDialogEvent: SingleLiveEvent<Boolean>
    fun get(showLoading: Boolean = true) = if (showLoading) this else null
    val isMainThread: Boolean
        get() = Looper.getMainLooper() == Looper.myLooper()

    fun showLoadingDialog(showLoading: Boolean = true) {
        if (showLoading) {
            if (isMainThread) {
                this.loadingDialogEvent.value = true
            } else {
                this.loadingDialogEvent.postValue(true)
            }
        }
    }

    fun hideLoadingDialog(showLoading: Boolean = true) {
        if (showLoading) {
            if (isMainThread) {
                this.loadingDialogEvent.value = false
            } else {
                this.loadingDialogEvent.postValue(false)
            }
        }
    }
}

interface IUICommandEvent {
    val binding: SingleLiveEvent<ViewDataBinding>
}

interface IUIChangeEvent : ILoadingDialogEvent, IUICommandEvent {
    val finishEvent: SingleLiveEvent<Unit>
    fun finishActivity() {
        if (isMainThread) {
            finishEvent.value = Unit
        } else {
            finishEvent.postValue(Unit)
        }
    }
}